Skip to content

perf(browser): pre-bundle the vitest runtime in optimizeDeps#10713

Open
sheremet-va wants to merge 1 commit into
mainfrom
perf/browser-mode
Open

perf(browser): pre-bundle the vitest runtime in optimizeDeps#10713
sheremet-va wants to merge 1 commit into
mainfrom
perf/browser-mode

Conversation

@sheremet-va

@sheremet-va sheremet-va commented Jul 5, 2026

Copy link
Copy Markdown
Member

Pre-bundles the vitest runtime for browser mode so the browser fetches a few optimized chunks instead of ~20 separately-served dist chunks (faster startup, fewer module requests).

vitest, vitest/internal/browser and @vitest/browser/client are moved from optimizeDeps.exclude to include. They are optimized together in a single pass, so esbuild dedupes their shared stateful chunks (the test collector, the runner, the RPC client) to a single instance — preserving module identity between the test files' import 'vitest' and the tester. Their transitive deps (@vitest/utils, @vitest/spy, pathe, tinyrainbow, …) are inlined into these bundles. The virtual per-server modules (@vitest/browser/context, …), vite/module-runner and msw must stay excluded (see the comment in index.ts).

Shared-server resolver fix. The vitest:resolve-core plugin (vitestResolver.ts) short-circuits bare vitest imports straight to the running copy's dist file. It now lives on the single Vite server shared by every environment, so it also governs the browser client environment where the runtime is pre-bundled — without care the tester would use the optimized bundle while test files receive the raw dist graph, producing two @vitest/runner instances and failing every test() with "Vitest failed to find the current suite". When the environment includes vitest in optimizeDeps, the plugin now routes the import through the optimizer (this.resolve) so both share one instance; every other environment keeps the pinned dist resolution.

Stack frames pointing into the optimizer cache (.vite/vitest) are ignored in stack traces like the un-bundled /vitest/dist/ ones. The console-logging spec now runs with an explicit default reporter and asserts on Vitest's own console forwarding: once the runtime is pre-bundled the tester loads a stubbed @vite/client, so Vite's client console relay no longer runs.

@hi-ogawa hi-ogawa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't look through yet, but it looks like it involves many independent changes. Is there a reasonable way to split PRs to ease reviews? (perhaps sourcemap related stuff?)

@sheremet-va

sheremet-va commented Jul 7, 2026

Copy link
Copy Markdown
Member Author

There are already 4 PRs stacked on top of each other 😩 It’s gonna be a merge headache. I’ll ask claude to split it 😔

@sheremet-va sheremet-va changed the base branch from perf/improve-loading-perf to main July 7, 2026 10:22
@netlify

netlify Bot commented Jul 7, 2026

Copy link
Copy Markdown

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit a163920
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6a4ce34561377b00083dcc4c
😎 Deploy Preview https://deploy-preview-10713--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

The browser now fetches a few optimized chunks instead of ~20
separately-served dist chunks. vitest, vitest/internal/browser and
@vitest/browser/client are optimized in a single pass so esbuild dedupes
their shared stateful chunks, preserving module identity between the test
files' import of 'vitest' and the tester. Frames from the optimizer cache
(.vite/vitest) are ignored in stack traces like the un-bundled dist ones.
@sheremet-va sheremet-va changed the title perf(browser): reduce headless run overhead perf(browser): pre-bundle the vitest runtime in optimizeDeps Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants